package com.sj.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sj.entity.Blog;
import com.sj.mapper.BlogMapper;
import com.sj.service.BlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 小七
 * @since 2021-07-30
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public IPage<Blog> listBlogByPage(Blog blog, Integer current, Integer size) {
        IPage<Blog> page = new Page<Blog>(current, size);
        IPage<Blog> blogPageInfo = blogMapper.listBlogByPage(page, blog);
        return blogPageInfo;
    }

    @Override
    public boolean bindTags(ArrayList<Integer> wantBindBlogTagIds, Integer id) {
        // 绑定之前删除该博客所有的标签 重新绑定标签
        blogMapper.deleteBlogAllTags(id);
        return blogMapper.bindTags(wantBindBlogTagIds, id) > 0;
    }

    @Override
    public boolean tagIsBindBlog(Integer tagId, Integer blogId) {
        return blogMapper.tagIsBindBlog(tagId, blogId) > 0;
    }

    @Override
    public Blog getBlogDetail(Integer blogId, Integer publicType, Integer userId) {
        return blogMapper.getBlogDetail(blogId, publicType, userId);
    }

    @Override
    public boolean deleteTagBindBlog(Integer tagId, Integer blogId) {
        return blogMapper.deleteTagBindBlog(tagId, blogId) > 0;
    }
}
